package week4th;

import java.io.Serializable;

public class Course implements Serializable,Comparable<Course>
{
	private String prefix;
	private int number;
	private String title;
	private String grade;

	public Course(String prefix, int number, String title, String grade)
	{
		this.prefix = prefix;
		this.number = number;
		this.title = title;
		if (grade == null)
			this.grade = "";
		else
			this.grade = grade;
	}

	public Course(String prefix, int number, String title)
	{
		this(prefix, number, title, "");
	}

	public String getPrefix()
	{
		return prefix;
	}

	public int getNumber()
	{
		return number;
	}

	public String getTitle()
	{
		return title;
	}

	public String getGrade()
	{
		return grade;
	}

	public void setGrade(String grade)
	{
		this.grade = grade;
	}

	public boolean taken()
	{
		return !grade.equals("");
	}

	public boolean equals(Object other)
	{
		boolean result = false;
		if (other instanceof Course)
		{
			Course otherCourse = (Course) other;
			if (prefix.equals(otherCourse.getPrefix()) &&
					number == otherCourse.getNumber())
				result = true;
		}
		return result;
	}

	public String toString()
	{
		String result = prefix + " " + number + ": " + title;
		if (!grade.equals(""))
			result += "  [" + grade + "]";
		return result;
	}

	@Override
	public int compareTo(Course o) {
		int result = 0;
		if (prefix.compareTo(o.getPrefix()) > 0) {
			result = 1;
		} else if (prefix.compareTo(o.getPrefix()) < 0) {
			result = -1;
		} else {
			if (number > o.number) {
				result = 1;
			} else if (number < o.number){
				result = -1;
			}
		}
		return result;
	}
}
